package org.etom.impl.ui.common;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.etom.connector.datatypes.IMoodleModule;

public class TreeViewerToolTipManager implements Listener {

  private static final String TABLE_ITEM_KEY = "_TREEITEM"; //$NON-NLS-1$
  
  private Tree tree;
  private Shell shell;
  private Display display;
  
  public TreeViewerToolTipManager(Tree tree, Shell shell, Display display) {
    this.tree = tree;
    this.shell = shell;
    this.display = display;
    // Disable native tool tip
    tree.setToolTipText (""); //$NON-NLS-1$
  }
  
  @Override
  public void handleEvent(Event event) {
    if (event.widget instanceof Label) {
      handleLabelEvent(event);
    } else {
      handleTreeEvent(event);
    }
  }

  private void handleLabelEvent(Event event) {
    Label label = (Label)event.widget;
    Shell shell = label.getShell ();
    switch (event.type) {
      case SWT.MouseDown:
        Event e = new Event ();
        e.item = (TreeItem) label.getData (TABLE_ITEM_KEY);
        // Assuming tree is single select, set the selection as if
        // the mouse down event went through to the table
        tree.setSelection (new TreeItem [] {(TreeItem) e.item});
        tree.notifyListeners (SWT.Selection, e);
        shell.dispose ();
        tree.setFocus();
        break;
      case SWT.MouseExit:
        shell.dispose ();
        break;
    }
  }
  
  private void handleTreeEvent(Event event) {
    Shell tip = null;
    Label label = null;
    switch (event.type) {
      case SWT.Dispose:
      case SWT.KeyDown:
      case SWT.MouseMove: {
        if (tip == null) break;
        tip.dispose ();
        tip = null;
        label = null;
        break;
      }
      case SWT.MouseHover: {
        TreeItem item = tree.getItem (new Point (event.x, event.y));
        if (item != null) {
          if (!(item.getData() instanceof IMoodleModule)) return;
          IMoodleModule moodleModule = (IMoodleModule) item.getData();
          String moduleDescription = (moodleModule != null) ?
              moodleModule.getDescription() : null;
          if ((moduleDescription == null) || (moduleDescription.trim().length() == 0))
            return;
          if (tip != null  && !tip.isDisposed ()) tip.dispose ();
          tip = new Shell (shell, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
          tip.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND));
          FillLayout layout = new FillLayout ();
          layout.marginWidth = 2;
          tip.setLayout (layout);
          label = new Label (tip, SWT.NONE);
          label.setForeground (display.getSystemColor (SWT.COLOR_INFO_FOREGROUND));
          label.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND));
          label.setData (TABLE_ITEM_KEY, item);
          label.setText (moodleModule.getDescription());
          label.addListener (SWT.MouseExit, this);
          label.addListener (SWT.MouseDown, this);
          Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT);
          Rectangle rect = item.getBounds (0);
          Point pt = tree.toDisplay (rect.x, rect.y);
          tip.setBounds (pt.x, pt.y, size.x, size.y);
          tip.setVisible (true);
        }
      }
    }
  }
  
}
